<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" class="w-full xl:full" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增账号</a-button>
        <a-button
          type="primary"
          danger
          @click="handleDelete"
          v-show="(deleteVisible = getSelectRowKeys().length > 0)"
          >批量删除</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:info-standard-line',
                tooltip: '查看用户详情',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑用户资料',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此账号',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDeleteRows.bind(null, record.id),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
    <!-- <Modal v-model:visible="deleteVisible" title="批量删除" >
      您确认要删除勾选的用户吗？
    </Modal> -->
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref } from 'vue'

  import { BasicTable, useTable, TableAction } from '/@/components/Table'
  import { listUser, removeUser } from '/@/api/demo/system'
  import { PageWrapper } from '/@/components/Page'
  import { useModal } from '/@/components/Modal'
  import AccountModal from './AccountModal.vue'
  import { useMessage } from '/@/hooks/web/useMessage'
  import { message } from 'ant-design-vue'
  import { columns, searchFormSchema } from './account.data'
  import { useGo } from '/@/hooks/web/usePage'
  import { useI18n } from '/@/hooks/web/useI18n'
  import { Modal } from 'ant-design-vue'
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
  import { createVNode } from 'vue'

  export default defineComponent({
    name: 'AccountManagement',
    components: { BasicTable, PageWrapper, AccountModal, TableAction, Modal },
    setup() {
      const deleteVisible = ref(false)
      const { createErrorModal } = useMessage()
      const { t } = useI18n()

      const go = useGo()
      const [registerModal, { openModal }] = useModal()
      const searchInfo = reactive<Recordable>({})
      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '账号列表',
        api: listUser,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        handleSearchInfoFn(info) {
          return info
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
        },
      })

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        })
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        })
      }

      function handleDelete() {
        Modal.confirm({
          title: () => '批量删除',
          icon: () => createVNode(ExclamationCircleOutlined),
          content: () => createVNode('div', { style: 'color:red;' }, '您想要删除这些用户吗?'),
          onOk() {
            handleDeleteRows()
          },
          onCancel() {
          },
          class: 'test',
        })
      }

      function handleSuccess() {
        reload()
      }

      function handleSelect(deptId = '') {
        searchInfo.deptId = deptId
        reload()
      }

      function handleView(record: Recordable) {
        go('/system/account_detail/' + record.id)
      }

      function handleDeleteRows(id?: string, _?: Event) {
        const rowsKey: string[] = id ? [id] : getSelectRowKeys()
        console.log('rowsKey', rowsKey)

        removeUser({ ids: rowsKey })
          .then(() => {
            message.success(t('sys.api.successMessage'))
            reload()
          })
          .catch((error) => {
            createErrorModal({
              title: t('sys.api.errorTip'),
              content: (error as unknown as Error).message || t('sys.api.networkExceptionMsg'),
            })
          })
          .finally()
      }

      return {
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleSelect,
        handleView,
        searchInfo,
        getSelectRowKeys,
        handleDeleteRows,
        deleteVisible,
      }
    },
  })
</script>
